/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kindatamanip;

import java.util.ArrayList;

/**
 *
        // how do we get an average genome?
        // the simple way is to, for each location, select the most common allele
        // other ways? each allele has a percentage frequency in the group,
        //           if we average the allelic frequencies, we get a measure of similarity
        // but will these still want 80%? can I come up with a modular way to compare within && between groups of DipOrgs,
        //           and use that to weight graphs?
 * @author thbrandston
 */
public class AverageGenome
{
    private ArrayList<int[][]> loci;
    private int[][] frequency;

    public DipOrg getDO()
    {
        if(loci == null)
            return null;

        int[][] gen = getGenome();
        DipOrg tmp = new DipOrg(gen.length, frequency[0].length);
        tmp.loci = gen;

        return tmp;
    }

    public int[][] getGenome()
    {
        int[][] genome = new int[frequency.length][2];

        int h1, h2;
        for(int i = 0; i < genome.length; i++)
        {
            h1 = Math.max(frequency[i][0], frequency[i][1]);
            h2 = Math.min(frequency[i][0], frequency[i][1]);
            for(int j = 2; j < frequency[i].length; j++)
            {
                if(frequency[i][j] > h1)
                {
                    h2 = h1;
                    h1 = frequency[i][j];
                }
            }
            genome[i][0] = h1;
            genome[i][1] = h2;
        }

        return genome;
    }

    public void addGenome(DipOrg gene)
    {
        if(loci == null)
        {
            loci = new ArrayList();
            frequency = new int[gene.loci.length][gene.variation];
        }

        loci.add(gene.loci);
        for(int i = 0; i < gene.loci.length; i++)
        {
            //use the value of the gene as an index
            //into the frequency array and increment
            frequency[i][gene.loci[i][0]]++;
            frequency[i][gene.loci[i][1]]++;
        }
    }
}